package com.location.footprint.fragment;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.location.footprint.R;
import com.location.footprint.adapter.CommentsAdapter;
import com.location.footprint.adapter.CommentsMoreAdapter;
import com.location.footprint.bean.CommentsBean;
import com.location.footprint.bean.CommentsChildBean;
import com.location.footprint.bean.CommentsTypeBean;
import com.location.footprint.bean.DataProvider;
import com.location.footprint.manager.DividerDecoration;
import com.location.footprint.utils.KeyBoardUtils;
import com.location.footprint.utils.LogUtil;
import com.location.footprint.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class CommentsFragment extends BaseFragment {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    Unbinder unbinder;
    private String mTitle;
    private CommentsAdapter hAdapter;
    private CommentsMoreAdapter moreAdapter;
    int index = 1;
    private Dialog dialog, sheetDialog;
    private EditText edit_chat;
    private ImageView img_send;
    private List<CommentsBean> commentsBeanList;
    private boolean isScrolling;

    public static CommentsFragment getInstance(String title) {
        CommentsFragment sf = new CommentsFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_hot_footprint;
    }

    @Override
    protected void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerview.setHasFixedSize(true);
        //color & height & paddingLeft & paddingRight
        DividerDecoration itemDecoration = new DividerDecoration(getContext().getResources().getColor(R.color.divider_gray), Utils.dip2px(getContext(), 1), Utils.dip2px(getContext(), 10), 10);
        recyclerview.addItemDecoration(itemDecoration);
        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING) {
                    isScrolling = true;
                    Glide.with(getContext()).pauseRequests();
                } else if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (isScrolling == true) {
                        Glide.with(getContext()).resumeRequests();
                    }
                    isScrolling = false;
                }
            }
        });
        commentsBeanList = DataProvider.getCommentsList(1);
        hAdapter = new CommentsAdapter(commentsBeanList);
        hAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
//        View endView = getLayoutInflater().inflate(R.layout.view_list_end, (ViewGroup) recyclerview.getParent(), false);
//        hAdapter.setFooterView(endView);
        recyclerview.setAdapter(hAdapter);
        hAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });

        hAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                TextView text_like = view.findViewById(R.id.text_like);
                switch (view.getId()) {
                    case R.id.text_like: //点赞
                        index++;
                        text_like.setText(index + "");
//                        ToastUtil.showToast(getContext().getApplicationContext(),"点赞");
                        break;
                    case R.id.text_comments: //评论
                        CommentsTypeBean bean = new CommentsTypeBean(CommentsTypeBean.COMMENTS_REPLY);
                        bean.setPosition(position);
                        showWriteCommentsDialog(bean);
                        break;
                    default:

                        break;

                }
            }
        });


    }

    private void showWriteCommentsDialog(CommentsTypeBean typeBean) {
        if (dialog == null) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.layout_keyboard, null);
            dialog = new Dialog(getContext());
            Window window = dialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(layoutParams);
            window.getDecorView().setMinimumWidth(getResources().getDisplayMetrics().widthPixels);
            window.getDecorView().setBackgroundColor(Color.WHITE);
            edit_chat = view.findViewById(R.id.edit_chat);
            img_send = view.findViewById(R.id.img_send);

            dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    KeyBoardUtils.showInput(getContext(), edit_chat);


                }
            });
            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    LogUtil.e("onDismiss....");


                }
            });
            dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    LogUtil.e("onCancel....");
                }
            });
//         dialog.setCancelable(false); //false 为禁止滑动
            dialog.setCanceledOnTouchOutside(true);  //false 点击外围是否消失
            dialog.setContentView(view);
        }
        switch (typeBean.getType()) {
            case CommentsTypeBean.COMMENTS_SUBMIT: //我的评论
                edit_chat.setHint("输入要说的内容");
                img_send.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CommentsBean bean = new CommentsBean(1);
                        bean.setNickName("想自由");
                        bean.setAvatarPath("http://q1.qlogo.cn/g?b=qq&s=640&nk=986483743");
                        bean.setContent("写的真不错，评论一下...");
                        commentsBeanList.add(bean);
                        recyclerview.scrollToPosition(commentsBeanList.size() - 1);
                        dialog.dismiss();

                    }
                });
                break;
            case CommentsTypeBean.COMMENTS_REPLY://回复一级评论
                edit_chat.setHint("回复：" + hAdapter.getItem(typeBean.getPosition()).getNickName());
                img_send.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        List<CommentsChildBean> list = new ArrayList<>();
                        CommentsChildBean childBean = new CommentsChildBean();
                        childBean.setNickName("龙哥快跑");
                        childBean.setComments("我来了，老弟");
                        list.add(childBean);
                        commentsBeanList.get(typeBean.getPosition()).getChildBeanList().add(childBean);
                        hAdapter.setData(typeBean.getPosition(), commentsBeanList.get(typeBean.getPosition()));
                        dialog.dismiss();

                    }
                });
                break;
            case CommentsTypeBean.COMMENTS_REPLY_CHILD://回复二级评论
                String nickName2 = commentsBeanList.get(typeBean.getPosition()).getChildBeanList().get(typeBean.getPosition2()).getNickName().split(",")[0];
                edit_chat.setHint("回复：" + nickName2);
                img_send.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        List<CommentsChildBean> list1 = new ArrayList<>();
                        CommentsChildBean childBean1 = new CommentsChildBean();
                        childBean1.setNickName("龙哥快跑2," + nickName2);
                        childBean1.setComments("这是2级评论，我嘞个去");
                        list1.add(childBean1);
                        commentsBeanList.get(typeBean.getPosition()).getChildBeanList().add(childBean1);
                        hAdapter.setData(typeBean.getPosition(), commentsBeanList.get(typeBean.getPosition()));
                        dialog.dismiss();

                    }
                });
                break;
            default:
                break;
        }

        dialog.show();


    }

    private void showCommentsMore() {
        if (sheetDialog == null) {
            sheetDialog = new BottomSheetDialog(getContext());
            View view = LayoutInflater.from(sheetDialog.getContext()).inflate(R.layout.layout_recyclerview, null);
            sheetDialog.setContentView(view);
            TextView textCancle = view.findViewById(R.id.text_cancle);
            textCancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sheetDialog.dismiss();
                }
            });
            RecyclerView recyclerview = view.findViewById(R.id.recyclerview);
            recyclerview.setLayoutManager(new LinearLayoutManager(sheetDialog.getContext()));
            recyclerview.setHasFixedSize(true);
            //color & height & paddingLeft & paddingRight
            DividerDecoration itemDecoration = new DividerDecoration(sheetDialog.getContext().getResources().getColor(R.color.divider_line), Utils.dip2px(getContext(), (float) 0.5), Utils.dip2px(getContext(), 0), 10);
            recyclerview.addItemDecoration(itemDecoration);
            moreAdapter = new CommentsMoreAdapter();
            moreAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                }
            });
            recyclerview.setAdapter(moreAdapter);
        }
        List<CommentsChildBean> commentsChildList = DataProvider.getCommentsChildList(8);
        moreAdapter.addData(commentsChildList);
        sheetDialog.show();
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (dialog != null) {
            dialog.cancel();
            dialog = null;
        }
        if (sheetDialog != null) {
            sheetDialog.cancel();
            sheetDialog = null;
        }
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        EventBus.getDefault().register(this);
        return rootView;
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void commentsOnClickListener(CommentsTypeBean bean) {

        showWriteCommentsDialog(bean);
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void commentsMoreOnClickListener(Integer position) {
        LogUtil.e("点了" + position);
        showCommentsMore();
    }
}